package service.impl;


import dao.AddCourseMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.CourseM;
import service.AddCourseService;

import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class AddCourseServiceImpl implements AddCourseService {
    @Autowired
    AddCourseMapper addCourseMapper;

    @Override
    public int addCourse(CourseM courseM) {
        log.info("courseM="+courseM);
        if(courseM.getId() == null){
            log.warn("CourseM缺少id,随机生成中");
            courseM.setId(UUID.randomUUID().toString());
        }
        return addCourseMapper.addCourse(courseM);
    }

    @Override
    public List<CourseM> getAll(String name) {
        return addCourseMapper.getAll("%"+name+"%");
    }

    @Override
    public List<CourseM> getCourseByArray(Integer currPage, Integer pageSize) {
        List<CourseM> courseMS = addCourseMapper.getAllByArray();
        int firstIndex = (currPage -1 )*pageSize;
        int lastIndex = currPage * pageSize;

        if((courseMS.size()-firstIndex)<pageSize){
            courseMS =courseMS.subList(firstIndex,courseMS.size());
        }
        try {
            courseMS = courseMS.subList(firstIndex, lastIndex);
        }catch (Exception e){
            e.printStackTrace();
        }
        return courseMS;
    }

    @Override
    public List<CourseM> getAllByArray() {
        List<CourseM> courseMS = addCourseMapper.getAllByArray();
        return courseMS;
    }

    @Override
    public Integer deleteCourse(String id) {

        return addCourseMapper.deleteCourse(id);
    }

}
